package com.gqj.meter.service.impl;

import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.meter.domain.MdWhInfo;
import com.gqj.meter.mapper.MdWhInfoMapper;
import com.gqj.meter.service.IMdWhInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;
import java.util.Map;

/**
 * 库房信息Service业务层处理
 *
 * @author lxq
 * @date lxq-01-09
 */
@Service
public class MdWhInfoServiceImpl implements IMdWhInfoService {
    @Autowired
    private MdWhInfoMapper mdWhInfoMapper;
    @Autowired
    protected Validator validator;
    private static final Logger log = LoggerFactory.getLogger(MdWhInfoServiceImpl.class);

    /**
     * 查询库房信息
     *
     * @param whId 库房信息主键
     * @return 库房信息
     */
    @Override
    public MdWhInfo selectMdWhInfoByWhId(Long whId) {
        return mdWhInfoMapper.selectMdWhInfoByWhId(whId);
    }

    /**
     * 查询库房信息列表
     *
     * @param mdWhInfo 库房信息
     * @return 库房信息
     */
    @Override
    public List<MdWhInfo> selectMdWhInfoList(MdWhInfo mdWhInfo) {
        return mdWhInfoMapper.selectMdWhInfoList(mdWhInfo);
    }

    /**
     * 新增库房信息
     *
     * @param mdWhInfo 库房信息
     * @return 结果
     */
    @Override
    public int insertMdWhInfo(MdWhInfo mdWhInfo) {
        return mdWhInfoMapper.insertMdWhInfo(mdWhInfo);
    }

    /**
     * 修改库房信息
     *
     * @param mdWhInfo 库房信息
     * @return 结果
     */
    @Override
    public int updateMdWhInfo(MdWhInfo mdWhInfo) {
        return mdWhInfoMapper.updateMdWhInfo(mdWhInfo);
    }

    /**
     * 批量删除库房信息
     *
     * @param whIds 需要删除的库房信息主键
     * @return 结果
     */
    @Override
    public int deleteMdWhInfoByWhIds(Long[] whIds) {
        return mdWhInfoMapper.deleteMdWhInfoByWhIds(whIds);
    }

    /**
     * 删除库房信息信息
     *
     * @param whId 库房信息主键
     * @return 结果
     */
    @Override
    public int deleteMdWhInfoByWhId(Long whId) {
        return mdWhInfoMapper.deleteMdWhInfoByWhId(whId);
    }

    /**
     * 查询库房下拉框
     *
     * @return 库房集合
     */
    public List<Map<String, Object>> mdWhInfoOptions() {
        return mdWhInfoMapper.mdWhInfoOptions();
    }

    /**
     * 导入库房数据
     *
     * @param mdWhInfoList    库房列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importMdWhInfo(List<MdWhInfo> mdWhInfoList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(mdWhInfoList) || mdWhInfoList.size() == 0) {
            throw new ServiceException("导入库房信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MdWhInfo whInfo : mdWhInfoList) {
            try {
                BeanValidators.validateWithException(validator, whInfo);
                // 验证是否存在这个数据whNo
                MdWhInfo w = mdWhInfoMapper.selectMdWhInfoByWhNo(whInfo.getWhNo());
                if (StringUtils.isNull(w)) {
                    //查询当前最大id
                    Long whId = mdWhInfoMapper.selectMaxId();
                    if (null == whId) {
                        whId = (long) 1;
                    } else {
                        whId = whId + 1;
                    }
                    whInfo.setWhId(whId);
                    this.insertMdWhInfo(whInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、库房编号 " + whInfo.getWhNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    whInfo.setWhId(w.getWhId());
                    this.updateMdWhInfo(whInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、库房编号 " + whInfo.getWhNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、库房编号 " + whInfo.getWhNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、库房编号 " + whInfo.getWhNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
